package org.polaris.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.polaris.authority.annotation.PolarisPermission;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.PageResult;
import org.polaris.org.domain.Department;
import org.polaris.org.query.DepartmentQuery;
import org.polaris.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "部门的API",description = "部门相关CRUD")
@RestController
@RequestMapping("/department")
@PolarisPermission(name="部门管理",descs = "部门管理")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @ApiOperation(value = "查询全部")
    @GetMapping
    @PolarisPermission(name="查询全部",descs = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.result().setObj(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID查询")
    @PolarisPermission(name="通过ID查询",descs = "通过ID查询")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.result().setObj(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID删除")
    @PolarisPermission(name="通过ID删除",descs = "通过ID删除")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.deleteById(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "批量删除")
    @PolarisPermission(name="批量删除",descs = "批量删除")
    @PatchMapping
    //除get请求其他请求传参应该加上requestBody否侧无数据
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "新增&修改,存在ID则修改反之则新增")
    @PutMapping
    @PolarisPermission(name="保存",descs = "新增&修改,存在ID则修改反之则新增")
    public AjaxResult save(@RequestBody Department department){
        if (StringUtils.isEmpty(department.getId())){
            try {
                departmentService.add(department);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }

        }else {
            try {
                departmentService.update(department);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }
        }
    }
    @ApiOperation(value = "分页")
    @PostMapping
    @PolarisPermission(name="分页",descs = "分页")
    public AjaxResult paging(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageResult paging = departmentService.paging(departmentQuery);
            System.out.println(paging);
            return AjaxResult.result().setObj(paging);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }

    }
    @ApiOperation(value = "查询部门树")
    @GetMapping("/tree")
    @PolarisPermission(name="查询部门树",descs = "查询部门树")
    public AjaxResult getDeptTree(){
        try {
            List<Department> deptTree = departmentService.getDeptTree();
            return AjaxResult.result().setObj(deptTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("查询部门树失败!");
        }
    }

}
